package com.bj58.data.emailreports.controllers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

//复用了周报里面的类
import com.bj58.data.emailreports.model.BWRAllOne;


import com.bj58.data.emailreports.model.BWRAllLineInc;
import com.bj58.data.emailreports.model.BWRAllOneJL;
import com.bj58.data.emailreports.model.BWRJzFluxFunnel;
import com.bj58.data.emailreports.model.BWRLineAccount;
import com.bj58.data.emailreports.model.BWRLineCityCate;
import com.bj58.data.emailreports.model.BWRLineCusTran;
import com.bj58.data.emailreports.model.BWRLineFlux;
import com.bj58.data.emailreports.model.BWRLineJTChange;
import com.bj58.data.emailreports.model.BWRLineSPIncome;
import com.bj58.data.emailreports.model.WRCusFunnelVo;

import com.bj58.data.emailreports.model.BMRNaturalFluxData;
import com.bj58.data.emailreports.model.BMRDemandAnalysisData;
import com.bj58.data.emailreports.model.onlineProductIncome;

import com.bj58.data.emailreports.service.BusiMonthReportService;

import com.bj58.data.emailreports.utils.DateUtil;
import com.bj58.data.emailreports.utils.EmailReportUtil;
import com.bj58.data.emailreports.utils.MailUtil;
import com.bj58.data.emailreports.utils.PatternUtil;
import com.bj58.data.emailreports.utils.ResourcesUtil;
import com.bj58.data.emailreports.utils.SomePublicUtil;
import com.bj58.data.emailreports.utils.SortUtil;
import com.bj58.data.emailreports.utils.SubmitSms;
import com.bj58.data.emailreports.utils.VelocityTools;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.PathUtil;

/**商业数据分析月报
 *
 */

public class BusiMonthReportController {

	private BusiMonthReportService busiMonthReportService= ServiceLocator.get(BusiMonthReportService.class);
	private EmailReportUtil eru = new EmailReportUtil();
	private SortUtil sortUtil = new SortUtil();
	private SomePublicUtil spu = new SomePublicUtil();
	private VelocityTools vt = new VelocityTools();
	
	/**第一部分  总体 - 进度/ 总体 - 简历 进度  / 收入趋势  /流量趋势 /页面类型流量趋势/精准各平台流量趋势
	 * @param searchDate
	 * @return
	 */
	public String monthReportPartOne(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/BusiMonthReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			
			String lastmonthfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",1);//查询日期（上月第一天）
			String lastyearfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",12);//查询日期（去年本月第一天）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(lastmonthfirstday);dateList.add(lastyearfirstday);
			
			List<String> dateList1 = new ArrayList<String>();  //没有去年 有的表不存在
			dateList1.add(today);dateList1.add(lastmonthfirstday);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastMonth", lastmonthfirstday);
			dateMap.put("lastYear",lastyearfirstday);
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("lastmonthfirstday", lastmonthfirstday.replaceAll("-", "."));
			conMap.put("lastyearfirstday", lastyearfirstday.replaceAll("-", "."));
				
			List<String> showCols = new ArrayList<String>(); //同比环比计算指标集
			List<String> percentList = new ArrayList<String>();//百分比指标集
		
			//总体 - 进度
			List<BWRAllOne> BMRAllOneList = busiMonthReportService.getBMRAllOneList(today);
			if(BMRAllOneList.get(0).getAllDone() == 0 || BMRAllOneList.get(0).getAllKpi() == 0){
				return "";
			}
			
			BMRAllOneList = sortUtil.sortListByFileNameVo(BMRAllOneList, BWRAllOne.class, "allDone", 2);
			conMap.put("BMRAllOneList", BMRAllOneList);
		
			//总体 - 简历 进度
			Map<String,BWRAllOneJL> BMRAllOneJLMap = busiMonthReportService.getBMRAllOneJLList(dateList1);
			conMap.put("BMRAllOneJLMap", BMRAllOneJLMap);
			conMap.put("jlToday", BMRAllOneJLMap.get(today));
			conMap.put("jlLastWeek", BMRAllOneJLMap.get(lastmonthfirstday));
			
			//分业务-收入趋势
			List<BWRAllLineInc> BMRAllLineIncList = busiMonthReportService.getBMRAllLineIncList(dateList1);
			
			showCols.clear();
			showCols.add("cost");showCols.add("cash");showCols.add("cusCount");showCols.add("ARPU");
			showCols.add("jzCost");showCols.add("jzCash");showCols.add("jzCusCount");showCols.add("jzARPU");
			showCols.add("zdCost");showCols.add("zdCash");showCols.add("zdCusCount");showCols.add("zdARPU");
			showCols.add("znCost");showCols.add("znCash");showCols.add("znCusCount");showCols.add("znARPU");
			
			List<String> BMRAllLineIncDatas = eru.conformDataTypeFive(BMRAllLineIncList, BWRAllLineInc.class, "busiLine", showCols, "statDate", null, dateList1);
			BMRAllLineIncDatas = SortUtil.sortListByFileName(BMRAllLineIncDatas, 1, 2);
			conMap.put("BMRAllLineIncDatas", BMRAllLineIncDatas);
					
			//分业务-流量趋势
			showCols.clear();
			showCols.add("pv");showCols.add("adpv");showCols.add("adimp");
			showCols.add("click");showCols.add("PVR");showCols.add("ASN");
			showCols.add("CPM");showCols.add("ACP");
			showCols.add("CTR2");		
			percentList.clear();
			percentList.add("PVR");percentList.add("CTR2");
			
			Map<String,List<String>> BMRLineFluxRS = new HashMap<String, List<String>>();		
			Map<String,List<BWRLineFlux>> BMRLineFluxMap= busiMonthReportService.getBMRAllLineFluxList(dateList1);
			for(Iterator<String> it = BMRLineFluxMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineFlux> tempList = BMRLineFluxMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFive(tempList, BWRLineFlux.class, "incomeType", showCols, "statDate", percentList, dateList1);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BMRLineFluxRS.put(busiLine, dataList);
			}
			conMap.put("BMRLineFluxRS", BMRLineFluxRS);
						
			//分业务-页面类型流量趋势
			showCols.clear();
			showCols.add("pv");showCols.add("adpv");showCols.add("adimp");
			showCols.add("click");showCols.add("PVR");showCols.add("ASN");
			showCols.add("CPM");showCols.add("ACP");
			showCols.add("CTR2");
			percentList.clear();
			percentList.add("PVR");percentList.add("CTR2");		
			Map<String,List<String>> BMRLinePageFluxRS = new HashMap<String, List<String>>();
			
			Map<String,List<BWRLineFlux>> BMRLinePageFluxMap= busiMonthReportService.getBMRLinePageFluxList(dateList1);
			for(Iterator<String> it = BMRLinePageFluxMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineFlux> tempList = BMRLinePageFluxMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFive(tempList, BWRLineFlux.class, "incomeType", showCols, "statDate", percentList, dateList1);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BMRLinePageFluxRS.put(busiLine, dataList);
			}
			conMap.put("BMRLinePageFluxRS", BMRLinePageFluxRS);
	
			//分业务-精准各平台流量趋势
			Map<String,List<String>> BMRLineJingzhunFluxRS = new HashMap<String, List<String>>();
			
			Map<String,List<BWRLineFlux>> BMRLineJingzhunFluxMap= busiMonthReportService.getBMRLineJingzhunFluxList(dateList1);
			for(Iterator<String> it = BMRLineJingzhunFluxMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineFlux> tempList = BMRLineJingzhunFluxMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFive(tempList, BWRLineFlux.class, "incomeType", showCols, "statDate", percentList, dateList1);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BMRLineJingzhunFluxRS.put(busiLine, dataList);
			}
			conMap.put("BMRLineJingzhunFluxRS", BMRLineJingzhunFluxRS);
						
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**第二部分 /余额客户结构
	 * @param searchDate
	 * @return
	 */
	public String monthReportPartTwo(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/BusiMonthReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			
			String lastmonthfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",1);//查询日期（上月第一天）
			String lastyearfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",12);//查询日期（去年本月第一天）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(lastmonthfirstday);dateList.add(lastyearfirstday);
			
			List<String> dateList1 = new ArrayList<String>();  //没有去年 有的表不存在
			dateList1.add(today);dateList1.add(lastmonthfirstday);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastMonth", lastmonthfirstday);
			dateMap.put("lastYear",lastyearfirstday);
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("lastmonthfirstday", lastmonthfirstday.replaceAll("-", "."));
			conMap.put("lastyearfirstday", lastyearfirstday.replaceAll("-", "."));
			
			
			List<String> showCols = new ArrayList<String>(); //同比环比计算指标集
	     	List<String> percentList = new ArrayList<String>();//百分比指标集
	     					
			//业务线 - 余额客户结构   和旧的保持一致...
			showCols.clear();
			showCols.add("allCus");showCols.add("left0");showCols.add("left50");
			showCols.add("cashLeft0");showCols.add("cashLeft50");
			showCols.add("preCus");showCols.add("preRate");
			showCols.add("topCus");showCols.add("topRate");
			showCols.add("idCus");showCols.add("idRate");
			
			percentList.clear();
			percentList.add("preRate");
			percentList.add("topRate");
			percentList.add("idRate");
			
			HashMap<String,List<String>>  cusFunnelMap = new HashMap<String, List<String>>();
			String cusFunnelChannel= "二手市场,招聘,黄页,二手车,房产";
//			String cusFunnelChannel= "二手市场";
			for (String channel : cusFunnelChannel.split(",")) {
				List<WRCusFunnelVo> cusFunnelList = busiMonthReportService.cusBanlanceDatas(dateList1,channel);
				List<String> cusFunnelDatas = eru.conformDataTypeFive(cusFunnelList, WRCusFunnelVo.class, "type", showCols, "statDate", percentList, dateList1);
				cusFunnelDatas = PatternUtil.mailWarnCheck(cusFunnelDatas, 10);
				cusFunnelDatas = SortUtil.sortListByFileName(cusFunnelDatas, 1, 2);
				
				cusFunnelMap.put(channel, cusFunnelDatas);
			}
			conMap.put("cusFunnelMap", cusFunnelMap);
			
				
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**第三部分 /消耗周期
	 * @param searchDate
	 * @return
	 */
	public String monthReportPartThree(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/BusiMonthReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			
			String lastmonthfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",1);//查询日期（上月第一天）
			String lastyearfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",12);//查询日期（去年本月第一天）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(lastmonthfirstday);dateList.add(lastyearfirstday);
			
			List<String> dateList1 = new ArrayList<String>();  //没有去年 有的表不存在
			dateList1.add(today);dateList1.add(lastmonthfirstday);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastMonth", lastmonthfirstday);
			dateMap.put("lastYear",lastyearfirstday);
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("lastmonthfirstday", lastmonthfirstday.replaceAll("-", "."));
			conMap.put("lastyearfirstday", lastyearfirstday.replaceAll("-", "."));   	
		
			//消耗周期 -- 和旧的保持一致即可
			HashMap<String,List<String>> consumCycleMap = new HashMap<String, List<String>>();
			HashMap<String,String> consumCycleCate2Map = new HashMap<String,String>();
			String channelsConsumCycle = "黄页,招聘,二手,二手车,房产";
//			String channelsConsumCycle = "黄页";
			
			String monthfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",0);//查询日期（本月第一天）
			
			for (String channel : channelsConsumCycle.split(",")) {
				String cate1 = "";
				String cate2 = "";
	            					
				if("黄页".equals(channel)){
					cate1 = " source_cate1_name in ('农林牧副渔','招商加盟','休闲娱乐','医疗健康','商务服务','婚庆摄影','家政服务','教育培训','汽车服务','旅游酒店','装修建材','餐饮美食','批发采购','丽人','公共服务') and source_cate2_name <> '二手回收' ";
					cate2 = "搬家,保洁清洗,保姆/月嫂,工商注册,家电维修,投资担保,租车,%家装服务%";
				}else if("招聘".equals(channel)){
					cate1 = " source_cate1_name in ('全职招聘','兼职招聘') ";
					cate2 = "";
				}else if("二手".equals(channel)){
					cate1 = "  ((source_cate1_name in ('二手市场','宠物','车辆买卖与服务','票务') and source_cate2_name !='二手车') or source_cate2_name = '二手回收') ";
					cate2 = "二手回收,二手手机,宠物狗,二手家具,二手摩托车,自行车/电动车,二手家电";
				}else if("房产".equals(channel)){
					cate1 = " source_cate1_name='房产信息' ";
					cate2 = "%二手房%,%租房%,%合租房%,%写字楼%,%商铺租售/生意转让%,%厂房/仓库/土地/车位%";
				}else if("二手车".equals(channel)){
					cate1 = " source_cate2_name = '二手车' ";
					cate2 = "";
				}
				List<String> cusFunnelList = busiMonthReportService.consumCycleData(today, monthfirstday, channel, cate1, cate2);
				cusFunnelList = SortUtil.sortListByFileName(cusFunnelList, 1, 2);
				consumCycleMap.put(channel, cusFunnelList);
				cate2 = cate2.replace("%", "");
				consumCycleCate2Map.put(channel, cate2);
			}
			conMap.put("consumCycleMap", consumCycleMap);
			conMap.put("consumCycleCate2Map", consumCycleCate2Map);
					
			
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**第四部分 /自然流量数据/供求分析/在线产品收入对比
	 * @param searchDate
	 * @return
	 */
	public String monthReportPartFour(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/BusiMonthReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			
			String lastmonthfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",1);//查询日期（上月第一天）
			String lastyearfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",12);//查询日期（去年本月第一天）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(lastmonthfirstday);dateList.add(lastyearfirstday);
			
			List<String> dateList1 = new ArrayList<String>();  //没有去年 有的表不存在
			dateList1.add(today);dateList1.add(lastmonthfirstday);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastMonth", lastmonthfirstday);
			dateMap.put("lastYear",lastyearfirstday);
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("lastmonthfirstday", lastmonthfirstday.replaceAll("-", "."));
			conMap.put("lastyearfirstday", lastyearfirstday.replaceAll("-", "."));
			
			
			List<String> showCols = new ArrayList<String>(); //同比环比计算指标集
	     	List<String> percentList = new ArrayList<String>();//百分比指标集
	     	
			//自然流量数据
			showCols.clear();
			showCols.add("Pv");showCols.add("Uv");showCols.add("Vppv");
			showCols.add("Rate");showCols.add("ListPv");
			
			Map<String,List<String>> BMRNaturalFluxRS = new HashMap<String, List<String>>();
			
			Map<String,List<BMRNaturalFluxData>> BMRNaturalFluxMap= busiMonthReportService.BMRNaturalFlux(dateList1);
			for(Iterator<String> it =BMRNaturalFluxMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BMRNaturalFluxData> tempList = BMRNaturalFluxMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFive(tempList, BMRNaturalFluxData.class, "type", showCols, "statDate", null, dateList1);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BMRNaturalFluxRS.put(busiLine, dataList);
			}
			conMap.put("BMRNaturalFluxRS", BMRNaturalFluxRS);
				
			//供求分析
			showCols.clear();
			showCols.add("CusDemand");showCols.add("BusDemand");showCols.add("JingzhunClickPer");
			showCols.add("ZhidingClickPer");
					
			List<BMRDemandAnalysisData> BMRDemandAnalysisList= busiMonthReportService.BMRDemandAnalysis(dateList);
			
			List<String> BMRDemandAnalysisRS= eru.conformDataTypeFour(BMRDemandAnalysisList, BMRDemandAnalysisData.class, "busiLine", showCols, "statDate", null, dateList);
			BMRDemandAnalysisRS = SortUtil.sortListByFileName(BMRDemandAnalysisRS, 1, 2);
			
			conMap.put("BMRDemandAnalysisRS", BMRDemandAnalysisRS);
					
			//在线产品收入对比
			Map<String,List<onlineProductIncome>> onlineProductIncomeMap=busiMonthReportService.getOnlineProductIncome(today);			
			Map<String,List<String>> onlineProductIncomeRS = new HashMap<String, List<String>>();		
			String yyyyMM = searchDate.substring(0,searchDate.lastIndexOf("-")).replace("-", "");
			
			for(Iterator<String> it = onlineProductIncomeMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				
				List<onlineProductIncome> tempList = onlineProductIncomeMap.get(busiLine);	
				List<String>  finalList = new ArrayList<String>();
				//整合数据									
				StringBuffer values = new StringBuffer();
				//总体数据
				values.append("总体;");	
				double Jzsum=0;
				double Zdsum=0;
				double Znsum=0;
				double Jzsum_lastyear=0;
				double Zdsum_lastyear=0;
				double Znsum_lastyear=0;
								
				int daysum=0;
				int daysum_lastyear=0;			
				for (onlineProductIncome vo : tempList) {
				
					if(vo.getTime().compareTo(yyyyMM)>0)continue;//如果是还没有到的月份，不参与算总体	
					String temp=vo.getTime().substring(0,4)+"-"+vo.getTime().substring(4,6);
		
					int day= DateUtil.getDaysByMonth(temp);
					daysum+=day;
					Jzsum+=vo.getJzCash()*day;
					Zdsum+=vo.getZdCash()*day;
					Znsum+=vo.getZnCash()*day;
									
					int day2= DateUtil.getMonthDays(DateUtil.getMonthBefore(temp,"yyyy-MM",12), "yyyyMM");
					daysum_lastyear+=day2;
					Jzsum_lastyear+=vo.getJzCash_lastyear()*day2;
					Zdsum_lastyear+=vo.getZdCash_lastyear()*day2;
					Znsum_lastyear+=vo.getZnCash_lastyear()*day2;
					
				}
				Jzsum=Jzsum/daysum;
				Zdsum=Zdsum/daysum;
				Znsum=Znsum/daysum;
			
				Jzsum_lastyear=Jzsum_lastyear/daysum_lastyear;
				Zdsum_lastyear=Zdsum_lastyear/daysum_lastyear;
				Znsum_lastyear=Znsum_lastyear/daysum_lastyear;
				
				values.append(vt.xsd(Jzsum)+";");values.append(vt.xsd(Jzsum_lastyear)+";");values.append(vt.tranRateTwo(Jzsum/Jzsum_lastyear-1)+";");
				values.append(vt.xsd(Zdsum)+";");values.append(vt.xsd(Zdsum_lastyear)+";");values.append(vt.tranRateTwo(Zdsum/Zdsum_lastyear-1)+";");
				values.append(vt.xsd(Znsum)+";");values.append(vt.xsd(Znsum_lastyear)+";");values.append(vt.tranRateTwo(Znsum/Znsum_lastyear-1)+";");
					
				finalList.add(values.toString());							
				for (onlineProductIncome vo : tempList) {
		
						if(vo.getTime().compareTo(yyyyMM)<=0)//如果是到的月份				
						{
							String temp = DateUtil.getMonthWord(vo.getTime().substring(4,6));
							values.delete(0, values.length()+1);
							values.append(temp+";");	
											
							values.append(vt.xsd(vo.getJzCash())+";");values.append(vt.xsd(vo.getJzCash_lastyear())+";");values.append(vt.tranRateTwo(vo.getJzRate())+";");
							values.append(vt.xsd(vo.getZdCash())+";");values.append(vt.xsd(vo.getZdCash_lastyear())+";");values.append(vt.tranRateTwo(vo.getZdRate())+";");
							values.append(vt.xsd(vo.getZnCash())+";");values.append(vt.xsd(vo.getZnCash_lastyear())+";");values.append(vt.tranRateTwo(vo.getZnRate())+";");
									
							finalList.add(values.toString());																
						}
						else		
						{
							String temp = DateUtil.getMonthWord(vo.getTime().substring(4,6));
							values.delete(0, values.length()+1);
							values.append(temp+";");	
											
							values.append("--;");values.append(vt.xsd(vo.getJzCash_lastyear())+";");values.append("--;");
							values.append("--;");values.append(vt.xsd(vo.getZdCash_lastyear())+";");values.append("--;");
							values.append("--;");values.append(vt.xsd(vo.getZnCash_lastyear())+";");values.append("--;");
									
							finalList.add(values.toString());																
						}	
				}
				onlineProductIncomeRS.put(busiLine, finalList);
		}			
			conMap.put("onlineProductIncomeRS", onlineProductIncomeRS);	
			
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**第五部分  /城市监控/类别监控/分平台收入变化/分来源收入变化/新增流失客户
	 * @param searchDate
	 * @return
	 */
	public String monthReportPartFive(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/BusiMonthReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			
			String lastmonthfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",1);//查询日期（上月第一天）
			String lastyearfirstday = DateUtil.firstDayMonthBefore(today, "yyyy-MM-dd",12);//查询日期（去年本月第一天）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(lastmonthfirstday);dateList.add(lastyearfirstday);
			
			List<String> dateList1 = new ArrayList<String>();  //没有去年 有的表不存在
			dateList1.add(today);dateList1.add(lastmonthfirstday);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastMonth", lastmonthfirstday);
	//		dateMap.put("lastYear",lastyearfirstday);
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("lastmonthfirstday", lastmonthfirstday.replaceAll("-", "."));
			conMap.put("lastyearfirstday", lastyearfirstday.replaceAll("-", "."));
			
			
			List<String> showCols = new ArrayList<String>(); //同比环比计算指标集
			List<String> percentList = new ArrayList<String>();//百分比指标集
			
			
			//业务线 - 城市监控
			showCols.clear();
			showCols.add("jzCash");showCols.add("jzCusCount");showCols.add("zdCash");
			showCols.add("zdCusCount");showCols.add("znCash");showCols.add("znCusCount");
			showCols.add("allPv");
			showCols.add("fluxTranRate");showCols.add("cusAdequacy");
			
			percentList.clear();
			percentList.add("fluxTranRate");percentList.add("cusAdequacy");
			
			Map<String,List<String>> BMRCitysRS = new HashMap<String, List<String>>();
			
			Map<String,List<BWRLineCityCate>> BMRCitysMap= busiMonthReportService.cityDatas(dateList1);
			for(Iterator<String> it = BMRCitysMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineCityCate> tempList = BMRCitysMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFive(tempList, BWRLineCityCate.class, "type", showCols, "statDate", percentList, dateList1);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BMRCitysRS.put(busiLine, dataList);
			}
			conMap.put("BMRCitysRS", BMRCitysRS);
				
			//业务线 - 类别监控
			showCols.clear();
			showCols.add("jzCash");showCols.add("jzCusCount");showCols.add("zdCash");
			showCols.add("zdCusCount");showCols.add("znCash");showCols.add("znCusCount");
			showCols.add("allPv");
			showCols.add("fluxTranRate");showCols.add("cusAdequacy");
			
			percentList.clear();
			percentList.add("fluxTranRate");percentList.add("cusAdequacy");
			
			Map<String,List<String>> BMRCatesRS = new HashMap<String, List<String>>();
			
			Map<String,List<BWRLineCityCate>> BMRCatesMap= busiMonthReportService.cateDatas(dateList1);
			for(Iterator<String> it = BMRCatesMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineCityCate> tempList = BMRCatesMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFive(tempList, BWRLineCityCate.class, "type", showCols, "statDate", percentList, dateList1);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BMRCatesRS.put(busiLine, dataList);
			}
			conMap.put("BMRCatesRS", BMRCatesRS);
			
						
			//业务线 - 分平台收入变化
			showCols.clear();
			showCols.add("jzPv");showCols.add("jzPvZb");showCols.add("jzCash");
			showCols.add("jzCashZb");showCols.add("jzTranRate");
			showCols.add("zdPv");showCols.add("zdPvZb");showCols.add("zdCash");
			showCols.add("zdCashZb");showCols.add("zdTranRate");
			
			percentList.clear();
			percentList.add("jzPvZb");percentList.add("jzCashZb");percentList.add("jzTranRate");
			percentList.add("zdPvZb");percentList.add("zdCashZb");percentList.add("zdTranRate");
			
			Map<String,List<String>> BMRLinePlatformRS = new HashMap<String, List<String>>();
			
			Map<String,List<BWRLineSPIncome>> BMRLinePlatformMap= busiMonthReportService.linePlatformIncomeDatas(dateList1);
			for(Iterator<String> it = BMRLinePlatformMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineSPIncome> tempList = BMRLinePlatformMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFive(tempList, BWRLineSPIncome.class, "type", showCols, "statDate", percentList, dateList1);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BMRLinePlatformRS.put(busiLine, dataList);
			}
			conMap.put("BMRLinePlatformRS", BMRLinePlatformRS);

			
			//业务线 - 分来源收入变化
			showCols.clear();
			showCols.add("jzPv");showCols.add("jzPvZb");showCols.add("jzCash");
			showCols.add("jzCashZb");showCols.add("jzTranRate");
			showCols.add("zdPv");showCols.add("zdPvZb");showCols.add("zdCash");
			showCols.add("zdCashZb");showCols.add("zdTranRate");
			
			percentList.clear();
			percentList.add("jzPvZb");percentList.add("jzCashZb");percentList.add("jzTranRate");
			percentList.add("zdPvZb");percentList.add("zdCashZb");percentList.add("zdTranRate");
			
			Map<String,List<String>> BMRLineSourceRS = new HashMap<String, List<String>>();
			
			Map<String,List<BWRLineSPIncome>> BMRLineSourceMap= busiMonthReportService.lineSourceIncomeDatas(dateList1);
			for(Iterator<String> it = BMRLineSourceMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineSPIncome> tempList = BMRLineSourceMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFive(tempList, BWRLineSPIncome.class, "type", showCols, "statDate", percentList, dateList1);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BMRLineSourceRS.put(busiLine, dataList);
			}
			conMap.put("BMRLineSourceRS", BMRLineSourceRS);
			
			
			//业务线 - 新增流失客户
			showCols.clear();
			showCols.add("showCus");showCols.add("costCus");showCols.add("costARPU");
			showCols.add("addCus");showCols.add("addARPU");
			showCols.add("lostCus");showCols.add("lostARPU");showCols.add("toJZ");
			showCols.add("toZD");showCols.add("lostZB");
			
			percentList.clear();
			percentList.add("toJZ");percentList.add("toZD");percentList.add("lostZB");
			
			Map<String,List<String>> BMRLineCusTranRS = new HashMap<String, List<String>>();
			
			Map<String,List<BWRLineCusTran>> BMRLineCusTranMap= busiMonthReportService.BMRLineCusTranDatas(dateList1);
			for(Iterator<String> it = BMRLineCusTranMap.keySet().iterator(); it.hasNext();){
				String busiLine = it.next();
				List<BWRLineCusTran> tempList = BMRLineCusTranMap.get(busiLine);
				List<String> dataList = eru.conformDataTypeFive(tempList, BWRLineCusTran.class, "incomeType", showCols, "statDate", percentList, dateList1);
				dataList = SortUtil.sortListByFileName(dataList, 1, 2);
				BMRLineCusTranRS.put(busiLine, dataList);
			}
			conMap.put("BMRLineCusTranRS", BMRLineCusTranRS);
						
					
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public static void main(String[] args) {
		try {
		String searchDate="2015-12-01";
			BusiMonthReportController test = new BusiMonthReportController();
		
			String content = test.monthReportPartFive(searchDate);
		//	String content = test.monthReportPartFour(searchDate);
		//	String content = test.monthReportPartTwo(searchDate);
		//	String content = test.monthReportPartThree(searchDate);
			if(!"".equals(content)){
				//MailUtil.javaMailSend("商业数据分析月报 from 2015-12-01 to 2015-12-31 part-4(自然流量数据/供求分析/在线产品收入对比) ", content, "ecdata@58.com",null);
				MailUtil.javaMailSend("商业数据分析月报 from 2015-12-01 to 2015-12-31 part-5(城市监控/类别监控/分平台收入变化/分来源收入变化/新增流失客户) ", content, "ecdata@58.com",null);
				
			}else{
				//邮件报警
			/*	content = "新商业周报报错！";
		    	MailUtil.javaMailSend("email report error", content, "huangliang@58.com",null);
		    	Map<String,String> telMap=new HashMap<String,String>();
		    	telMap.put("13811583416","email report error:"+content);
		    	SubmitSms.submitMsg(telMap);
		    		    	
			*/
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
